﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class WakeUpConfigDeadBody : WakeUpConfigStartPosAndOri {
    public Sprite Sp;
    public bool IsCameraFollow;
    public WakeUpConfigDeadBody(Vector2 pos ,Vector2 ori,Sprite sp,bool isCameraFollowTarget = false):base(pos, ori) {
        this.Sp = sp;
        this.IsCameraFollow = isCameraFollowTarget;
    }
}

public class EnemyDeadBody : PrefabCommonBase, ICameraFollowTarget
{

    public float SpeedValue = 4;
    public float AngleSpeedValue = 10;
    public float BodyExistTime = 5;

    public EMoveOri GetMoveOri()
    {
        return EMoveOri.E_MID;
    }

    public Vector2 GetPosition()
    {
        return this.transform.localPosition;
    }

    public override void SetStatusByConfig(WakeUpConfigBase config)
    {
        var wc = config as WakeUpConfigDeadBody;
        if (wc != null) {
            var rig2d = this.GetComponent<Rigidbody2D>();
            this.transform.localPosition = new Vector3(wc.StartPos.x,wc.StartPos.y, this.transform.localPosition.z);
            var oriValue = wc.Ori.x;
            var sendOri = new Vector2(1*oriValue,1).normalized;
            rig2d.velocity = new Vector2(sendOri .x* this.SpeedValue, sendOri .y* this.SpeedValue);
            rig2d.angularVelocity = AngleSpeedValue * oriValue;
            var spr = this.GetComponent<SpriteRenderer>();
            spr.sprite = wc.Sp;
            if (wc.IsCameraFollow) {
                CameraMgr.GetInstance().CameraFollowTarget(this);
            }
        }
    }

    protected override void onActive()
    {
        var rig2d = this.GetComponent<Rigidbody2D>();
        this.gameObject.SetActive(true);
        rig2d.WakeUp();
        this.CancelInvoke();
        this.Invoke("onDeadOver", BodyExistTime);
    }

    protected override void onSleep()
    {
        var rig2d = this.GetComponent<Rigidbody2D>();
        this.gameObject.SetActive(false);
        rig2d.Sleep();
    }

    private void onDeadOver() {
        this.State = EnumItemState.E_SLEEP;
    }
}
